﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MagicCove
{
    public partial class ReorderListForm : Form
    {
        private List<MagicCard> reorderedList;
        internal ReorderListForm(List<MagicCard> reorderedList)
        {
            InitializeComponent();
            this.reorderedList = reorderedList;
            if (reorderedList != null && reorderedList.Count > 0)
                updateListBox(0);
        }
        private void updateListBox(int selectedItem)
        {
            reorderListLB.Items.Clear();
            foreach (MagicCard mC in reorderedList)
                reorderListLB.Items.Add(mC.CardName);
            reorderListLB.SelectedIndex = selectedItem;
        }

        internal List<MagicCard> ReorderedList
        {
            get { return reorderedList; }
        }
        private void upB_Click(object sender, EventArgs e)
        {
            if (reorderListLB.SelectedIndex == -1)
                MessageBox.Show("Please select an item to move up.", "Error - No Item Selected");
            else if (reorderListLB.SelectedIndex == 0)
            {
                ;//do nothing
            }
            else if (reorderListLB.SelectedIndex == 1)
            {
                reorderedList.Insert(0, reorderedList[1]);
                reorderedList.RemoveAt(2);
                updateListBox(0);
            }
            else
            {
                reorderedList.Insert(reorderListLB.SelectedIndex - 1, reorderedList[reorderListLB.SelectedIndex]);
                reorderedList.RemoveAt(reorderListLB.SelectedIndex + 1);
                updateListBox(reorderListLB.SelectedIndex - 1);
            }
        }

        private void downB_Click(object sender, EventArgs e)
        {
            if (reorderListLB.SelectedIndex == -1)
                MessageBox.Show("Please select an item to move down.", "Error - No Item Selected");
            else if (reorderListLB.SelectedIndex == reorderedList.Count - 1)
            {
                ;//do nothing
            }
            else if (reorderListLB.SelectedIndex == reorderedList.Count - 2)
            {
                reorderedList.Add(reorderedList[reorderListLB.SelectedIndex]);
                reorderedList.RemoveAt(reorderListLB.SelectedIndex);
                updateListBox(reorderedList.Count - 1);
            }
            else
            {
                reorderedList.Insert(reorderListLB.SelectedIndex + 2, reorderedList[reorderListLB.SelectedIndex]);
                reorderedList.RemoveAt(reorderListLB.SelectedIndex);
                updateListBox(reorderListLB.SelectedIndex + 1);
            }
        }

        private void closeB_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
